<?php

	/**
	 * PHP IDS
	 * 
	 * Requirements: PHP5, SimpleXML
	 *  
	 * Copyright 2007 Mario Heiderich for Ormigo 
	 * 
	 * Permission is hereby granted, free of charge, to any person obtaining a copy 
	 * of this software and associated documentation files (the "Software"), to deal 
	 * in the Software without restriction, including without limitation the rights to use, 
	 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the 
	 * Software, and to permit persons to whom the Software is furnished to do so, 
	 * subject to the following conditions:
	 * 
	 * The above copyright notice and this permission notice shall be included in 
	 * all copies or substantial portions of the Software.
	 * 
	 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
	 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
	 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
	 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
	 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 
	 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
	 */
	
	/**
	 * This file is hosted on Google Code and can be 
	 * discussed on Google Groups
	 * 
	 * http://code.google.com/p/phpids/ 
	 * http://groups.google.de/group/php-ids/
	 * 
	 */
	
	require_once 'log_interface.php';
	
	/**
	 * Callback logger
	 *
	 * This class provides a callback based logger. You can add callbacks
	 * and specify on which impact they should be called. Also a generic
	 * callback (default behaviour) is provided.
	 *
	 * @author	Lars Strojny <lstrojny@neu.de>
	 */
	class IDS_Log_Callback implements IDS_Log_Interface
	{
		/**
		 * List of callbacks
		 *
		 * @var		array
		 * @access	protected
		 */
		protected $_callbacks = array();
	
		/**
		 * Execute logging
		 *
		 * @access	public
		 * @param	array	$data
		 * @return	object	$this
		 */
		public function execute($data)
		{
			if (empty($this->_callbacks)) {
				throw new Exception('No callback(s) specified');
			}
			foreach ($this->_callbacks as $callback) {
				// General callback
				if ($callback[1] === null) {
					call_user_func_array($callback[0], array($data));
				} else {
					$impact = 0;
					foreach ($data as $row) {
						foreach ($row['filter'] as $filter) {
							if (isset($filter['impact'])) {
								$impact += (int)$new_impact;
							}
						}
					}
					if (version_compare($impact, $callback[1], $callback[2])) {
						call_user_func_array($callback[0], array($data));
					}
				}
			}
			
			return $this;
		}
	
		/**
		 * Add callback
		 *
		 * Add a certain callback either a global one (when leaving $impact null)
		 * or for specified impact levels.
		 *
		 * @access	public
		 * @param 	callback 		$callback
		 * @param 	void|integer 	$impact
		 * @param 	string			$operator >, <, >= or <=
		 * @return	object			$this or exception on failure
		 */
		public function addCallback($callback, $impact = null, $operator = '>=')
		{
			if (!is_callable($callback)) {
				throw new Exception('Invalid callback given');
			}
	
			if (!in_array($operator, array('>', '<', '>=', '<='))) {
				throw new Exception(
					'Invalid operator given. Choose one of those: >, <, >=, <='
				);
			}
			$impact = $impact === null ? $impact : (int)$impact;
			$this->_callbacks[] = array($callback, $impact, $operator);
			return $this;
		}
	}
